#include "CoinManager.h"
#include "ConfigManager.h"
#include "ResourcesManager.h"

CoinManager::CoinManager()
{
    numCoin = ConfigManager::Instance().num_initial_coin;
}

CoinManager::~CoinManager()
{
    for (CoinProp *coin_prop: coinPropList)
        delete coin_prop;
}

void CoinManager::IncreaseCoin(double val)
{
    numCoin += val;
}

void CoinManager::DecreaseCoin(double val)
{
    numCoin -= val;

    if (numCoin < 0) {
        numCoin = 0;
    }
}

void CoinManager::OnUpdate(double delta)
{
    for (CoinProp *coin_prop: coinPropList)
        coin_prop->OnUpdate(delta);
    coinPropList.erase(std::remove_if(coinPropList.begin(), coinPropList.end(),
                                      [](CoinProp *coinProp) {
                                          bool deletable = coinProp->CanRemove();
                                          if (deletable) delete coinProp;
                                          return deletable;
                                      }), coinPropList.end());
}

void CoinManager::OnRender(SDL_Renderer *renderer)
{
    for (CoinProp *coin_prop: coinPropList)
        coin_prop->OnRender(renderer, ResourcesManager::Instance().texturePool[ResID::Tex_Coin]);
}

double CoinManager::GetCurrentCoinNum()
{
    return numCoin;
}

std::vector<CoinProp *> &CoinManager::GetCoinPropList()
{
    return coinPropList;
}

void CoinManager::SpawnCoinProp(const Vector2 &position)
{
    auto *coin_prop = new CoinProp();
    coin_prop->SetPosition(position);
    coinPropList.push_back(coin_prop);
}